package com.thinkit.bigdata.web.controller.bus;

import com.google.common.base.Strings;
import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.core.util.DateFormatUtil;
import com.thinkit.bigdata.core.util.es.ESUtil;
import com.thinkit.bigdata.core.util.excel.XlsUtil;
import com.thinkit.bigdata.web.model.BaseEntity;
import com.thinkit.bigdata.web.model.bus.BusWorkTest;
import com.thinkit.bigdata.web.model.bus.BusWorkTestCopy;
import com.thinkit.bigdata.web.model.bus.BusWorkTestExample;
import com.thinkit.bigdata.web.model.bus.BusWorkTestWithBLOBs;
import com.thinkit.bigdata.web.model.es.audioEs.VoiceInfoDto;
import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.model.result.HtmlResult;
import com.thinkit.bigdata.web.model.sec.User;
import com.thinkit.bigdata.web.service.bus.BusModelRuleService;
import com.thinkit.bigdata.web.service.bus.BusWorkTestService;
import com.thinkit.bigdata.web.service.config.AreaService;
import com.thinkit.bigdata.web.service.sec.UserService;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型测检任务
 * Created by hccl on 2018/10/12.
 */
@RestController
@RequestMapping("/test-task")
public class BusWorkTestContr {

    private static Logger logger = Logger.getLogger(BusWorkTestContr.class);

    @Resource
    private BusWorkTestService busWorkTestService;

    @Resource
    private BusModelRuleService busModelRuleService;

    @Resource
    private AreaService areaService;

    @Resource
    private UserService userService;

    @Resource
    private ESUtil esUtil;

    @RequestMapping(value = "/get-data-list", method = RequestMethod.POST)
    public HtmlResult getPerTaskData(HttpServletRequest request) {
        String logTxt = "【模型测检任务分页查询】";
        logger.info(logTxt);
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));//起始页
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页条数
        String sortName = request.getParameter("sortName");//排序字段
        String sortOrder = request.getParameter("sortOrder");//排除方式
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        logger.info(logTxt + "sortName = " + sortName);
        logger.info(logTxt + "sortOrder = " + sortOrder);
        Page<BusWorkTest> page = new Page<>(pageNum + 1, pageSize);
        Map<String,Object> map = getDataQuery(request, logTxt);
        return HtmlResult.success(busWorkTestService.selectWorkInfos(page, map), page.getTotalCount());
    }

    private Map<String,Object> getDataQuery(HttpServletRequest request, String logTxt) {
        String provinceCode = request.getParameter("provinceCode");//省份标识
        String modelId = request.getParameter("modelId");//名称
        String createTimeBegin = request.getParameter("createTimeBegin");//创建时间
        String createTimeEnd = request.getParameter("createTimeEnd");//创建时间
        String onsetTimeBegin = request.getParameter("onsetTimeBegin");//发布时间
        String onsetTimeEnd = request.getParameter("onsetTimeEnd");//发布时间
        String callStartTime = request.getParameter("callStartTime");//通话开始时间
        String callEndTime = request.getParameter("callEndTime");//通话结束时间
        String createUserId = request.getParameter("createUserId");//创建人
        String proStatus = request.getParameter("proStatus");//处理状态
        String sortName = request.getParameter("sortName");//排序字段
        String sortOrder = request.getParameter("sortOrder");//排除方式
        logger.info(logTxt + "sortName = " + sortName);
        logger.info(logTxt + "sortOrder = " + sortOrder);
        logger.info(logTxt + "createTimeBegin = " + createTimeBegin);
        logger.info(logTxt + "createTimeEnd = " + createTimeEnd);
        logger.info(logTxt + "onsetTimeBegin = " + onsetTimeBegin);
        logger.info(logTxt + "onsetTimeEnd = " + onsetTimeEnd);
        logger.info(logTxt + "callStartTime = " + callStartTime);
        logger.info(logTxt + "callEndTime = " + callEndTime);
        logger.info(logTxt + "provinceCode = " + provinceCode);
        logger.info(logTxt + "modelId = " + modelId);
        logger.info(logTxt + "proStatus = " + proStatus);
        logger.info(logTxt + "createUserId = " + createUserId);
        Map<String,Object> map = new HashMap<>();
        if (!Strings.isNullOrEmpty(createUserId)) {
            map.put("createUserId", Arrays.asList(createUserId.split(",")));
        } else {
            Session session = SecurityUtils.getSubject().getSession();
            int userIdentity = (Integer) session.getAttribute("userIdentity");
            if (BaseEntity.USERIDENTITY_ZJ == userIdentity) {//质检员,看到组员包括自己创建的数据
                User user = (User) session.getAttribute(Constants.SESSION_USER_INFO);
                map.put("createUserId", userService.selectMemberIdByLeaderId(user.getId()));
            }
        }
        if (!Strings.isNullOrEmpty(createTimeBegin)) {
            map.put("createTimeBegin", DateFormatUtil.stringToDate(createTimeBegin, DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(createTimeEnd)) {
            map.put("createTimeEnd", DateFormatUtil.stringToDate(createTimeEnd, DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(onsetTimeBegin)) {
            map.put("onsetTimeBegin", DateFormatUtil.stringToDate(onsetTimeBegin, DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(onsetTimeEnd)) {
            map.put("onsetTimeEnd", DateFormatUtil.stringToDate(onsetTimeEnd, DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(callStartTime)) {
            map.put("callStartTime", DateFormatUtil.stringToDate(callStartTime, DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(callEndTime)) {
            map.put("callEndTime", DateFormatUtil.stringToDate(callEndTime, DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(provinceCode)) {
            map.put("provinceCode", Arrays.asList(provinceCode.split(",")));
        }
        if (!Strings.isNullOrEmpty(proStatus)) {
            map.put("proStatus", Arrays.asList(proStatus.split(",")));
        }
        if (!Strings.isNullOrEmpty(modelId)) {
            map.put("modelId", Arrays.asList(modelId.split(",")));
        }
        String orderBy = "lastUpdateTime desc";
        if (!Strings.isNullOrEmpty(sortName)) {
            orderBy = sortName + " " + sortOrder;
        }
        map.put("orderBy", orderBy);
        return map;
    }

    @RequestMapping(value = "/export-model-task-data", method = RequestMethod.POST)
    public void exportModelTaskData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【模型测检任-导出】";
        logger.info(logTxt);
        try {
            Map<String,Object> map = getDataQuery(request, logTxt);
            List<BusWorkTestCopy> works = busWorkTestService.selectWorkInfos(map);
            logger.info("获取数据条数为：" + works.size());
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "省份", "模型名称", "创建人", "状态",
                    "通话时间段", "启动时间", "最后更新时间", "备注"};
            String title = "模型测检任务信息表";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < works.size(); j++) {
                BusWorkTestCopy source = works.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString(j + ""));
                            break;
                        case 1:
                            cell2.setCellValue(new HSSFRichTextString(areaService.selectByPrimaryCode(source.getProvinceCode()).getArea()));
                            break;
                        case 2:
                            cell2.setCellValue(new HSSFRichTextString(source.getModelName()));
                            break;
                        case 3:
                            cell2.setCellValue(new HSSFRichTextString(source.getCreateuserName()));
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(esUtil.changeTaskStatus(source.getProStatus())));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(
                                    DateFormatUtil.dateToString(source.getCallStartTime(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss)
                                    + "-" +
                                            DateFormatUtil.dateToString(source.getCallEndTime(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss)
                            ));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(DateFormatUtil.dateToString(source.getOnsetTime(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss)));
                            break;
                        case 7:
                            cell2.setCellValue(new HSSFRichTextString(DateFormatUtil.dateToString(source.getLastUpdateTime(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss)));
                            break;
                        case 8:
                            cell2.setCellValue(new HSSFRichTextString(source.getDescription()));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.info(logTxt + "异常");
        }
    }

    @RequestMapping(value = "/get-rule-mes-bu-model", method = RequestMethod.POST)
    public HtmlResult getRuleMesByModelId(@RequestParam(value = "modelId") final Long modelId) {
        String logTxt = "【根据模型id获取规则信息】";
        logger.info(logTxt);
        logger.info(logTxt + "modelId = " + modelId);
        return HtmlResult.success(busModelRuleService.getRuleMesByModelId(modelId));
    }

    @RequestMapping(value = "/get-add-test-task-meg", method = RequestMethod.POST)
    public HtmlResult getAddTestTaskMeg(HttpServletRequest request) {
        String logTxt = "【模型测检添加-获取音频列表-分页】";
        logger.info(logTxt);
        String pageNum = request.getParameter("pageNum");//起始页
        String pageSize = request.getParameter("pageSize");//每页条数
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        return busWorkTestService.getAddTestTaskMeg(request);
    }

    @RequestMapping(value = "/add-test-task", method = RequestMethod.POST)
    public HtmlResult addTestTask(HttpServletRequest request) {
        String logTxt = "【添加质检任务】";
        logger.info(logTxt);
        String maxItems = request.getParameter("maxItems");//最多条目数
        String modelIds = request.getParameter("modelIds");//模型ID
        logger.info(logTxt + "maxItems = " + maxItems);
        logger.info(logTxt + "modelIds = " + modelIds);
        return busWorkTestService.insertSelective(request);
    }

    @RequestMapping(value = "/update-data", method = RequestMethod.POST)
    public HtmlResult updateTask(HttpServletRequest request) {
        String logTxt = "【修改质检任务】";
        logger.info(logTxt);
        String id = request.getParameter("id");//id
        logger.info(logTxt + "id = " + id);
        return busWorkTestService.updateByPrimaryKeySelective(request);
    }

    @RequestMapping(value = "/del-data-by-id", method = RequestMethod.POST)
    public HtmlResult delPerTask(HttpServletRequest request) {
        String logTxt = "【删除任务】";
        logger.info(logTxt);
        String ids = request.getParameter("ids");//任务ids
        logger.info(logTxt + "ids = " + ids);
        return busWorkTestService.deleteByPrimaryKey(ids);
    }

    @RequestMapping(value = "/start-or-stop-task", method = RequestMethod.POST)
    public HtmlResult startTask(HttpServletRequest request) {
        String logTxt = "【启动任务】";
        logger.info(logTxt);
        String id = request.getParameter("id");//任务id
        String startOrStop = request.getParameter("startOrStop");//启动/停止
        logger.info(logTxt + "id = " + id);
        logger.info(logTxt + "startOrStop = " + startOrStop);
        try {
            return busWorkTestService.startTask(request);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(logTxt + Arrays.toString(e.getStackTrace()));
            return HtmlResult.failure("1".equals(startOrStop) ? "启动" : "停止" +"失败");
        }
    }

    @RequestMapping(value = "/get-tasks", method = RequestMethod.POST)
    public HtmlResult getTaskList() {
        String logTxt = "【获取全部任务信息列表】";
        logger.info(logTxt);
        BusWorkTestExample example = new BusWorkTestExample();
        example.createCriteria().andIdIsNotNull();
        return HtmlResult.success(busWorkTestService.selectByExample(example));
    }

    @RequestMapping(value = "/get-model-bu-ares", method = RequestMethod.POST)
    public HtmlResult getModelList(@RequestParam(value = "areaInfo") final String areaInfo) {
        String logTxt = "【获取模型信息列表】";
        logger.info(logTxt);
        Map<String, Object> map = new HashMap<>();

        if (!Strings.isNullOrEmpty(areaInfo)) {
            map.put("provinceCode", Arrays.asList(areaInfo.split(",")));
        }
        Session session = SecurityUtils.getSubject().getSession();
        int userIdentity = (Integer) session.getAttribute("userIdentity");
        if (BaseEntity.USERIDENTITY_ZJ == userIdentity) {//质检员,看到组员包括自己创建的数据
            User user = (User) session.getAttribute(Constants.SESSION_USER_INFO);
            map.put("createUserId", userService.selectMemberIdByLeaderId(user.getId()));
        }
        return HtmlResult.success(busWorkTestService.selectDateList(map));
    }

    @RequestMapping(value = "/work-uniqueness", method = RequestMethod.POST)
    public HtmlResult workUniqueness(HttpServletRequest request) {
        String logTxt = "【任务名称唯一性验证】";
        logger.info(logTxt);
        String id = request.getParameter("id");//任务id
        String workName = request.getParameter("workName");//任务name
        logger.info(logTxt + "id = " + id);
        logger.info(logTxt + "workName = " + workName);
        BusWorkTestExample example = new BusWorkTestExample();
        example.createCriteria().andTestworknameEqualTo(workName);
        List<BusWorkTestWithBLOBs> works = busWorkTestService.selectByExample(example);
        if (!works.isEmpty()) {
            if (!Strings.isNullOrEmpty(id)) {
                BusWorkTestExample example1 = new BusWorkTestExample();
                example1.createCriteria().andTestworknameEqualTo(workName).andIdEqualTo(Long.parseLong(id));
                List<BusWorkTestWithBLOBs> busWorks = busWorkTestService.selectByExample(example1);
                if (!busWorks.isEmpty()) {
                    return HtmlResult.success();
                }
            }
            return HtmlResult.failure("任务名称已存在");
        }
        return HtmlResult.success();
    }

    @RequestMapping(value = "/get-audio-list", method = RequestMethod.POST)
    public HtmlResult getAudioList(HttpServletRequest request) {
        String logTxt = "【模型质检任务详情-统计】";
        logger.info(logTxt);
        String id = request.getParameter("id");//任务id
        logger.info(logTxt + "id = " + id);
        return busWorkTestService.selectByExample(request);
    }

    @RequestMapping(value = "/get-audio-message", method = RequestMethod.POST)
    public HtmlResult getAudioMessage(HttpServletRequest request) {
        String logTxt = "【根据条件查询满足规则的音频信息-分页】";
        logger.info(logTxt);
        String maxItems = request.getParameter("maxItems");//最多条目数
        String pageNum = request.getParameter("pageNum");//起始页
        String pageSize = request.getParameter("pageSize");//每页条数
        logger.info(logTxt + "maxItems = " + maxItems);
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        return busWorkTestService.getAudioMessage(request, logTxt);
    }

    @RequestMapping(value = "/get-audio-list-task-id", method = RequestMethod.POST)
    public HtmlResult getAudioListById(HttpServletRequest request) {
        String logTxt = "【根据任务id获取音频信息】";
        logger.info(logTxt);
        String id = request.getParameter("id");//任务id
        logger.info(logTxt + "id = " + id);
        return busWorkTestService.getAudioListById(request);
    }

    @RequestMapping(value = "/get-model-list-task-id", method = RequestMethod.POST)
    public HtmlResult getModelListById(HttpServletRequest request) {
        String logTxt = "【根据任务id获取模型信息】";
        logger.info(logTxt);
        String id = request.getParameter("id");//任务id
        logger.info(logTxt + "id = " + id);
        return busWorkTestService.getModelListById(request);
    }

    @RequestMapping(value = "/get-rule-detail-data", method = RequestMethod.POST)
    public HtmlResult getRuleDetailData(HttpServletRequest request) {
        String logTxt = "【模型检测结果明细-规则命中明细】";
        logger.info(logTxt);
        String workId = request.getParameter("workId");
        String modelId = request.getParameter("modelId");
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));//起始页
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页条数
        String sortName = request.getParameter("sortName");//排序字段
        String sortOrder = request.getParameter("sortOrder");//排除方式
        logger.info(logTxt + "workId = " + workId);
        logger.info(logTxt + "modelId = " + modelId);
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        logger.info(logTxt + "sortName = " + sortName);
        logger.info(logTxt + "sortOrder = " + sortOrder);
        return busWorkTestService.getDetailData(request);
    }

    @RequestMapping(value = "/get-phone-detail-data", method = RequestMethod.POST)
    public HtmlResult getPhoneDetailData(HttpServletRequest request) {
        String logTxt = "【模型检测结果明细-话单明细】";
        logger.info(logTxt);
        String serialNumber = request.getParameter("serialNumber");//录音流水
        String satisfactions = request.getParameter("satisfaction");//满意度
        String workId = request.getParameter("workId");
        String ruleId = request.getParameter("ruleId");
        String isHit = request.getParameter("isHit");//是否命中：是(YES)；否(NO)
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));//起始页
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//每页条数
        String sortName = request.getParameter("sortName");//排序字段
        String sortOrder = request.getParameter("sortOrder");//排除方式
        logger.info(logTxt + "serialNumber = " + serialNumber);
        logger.info(logTxt + "satisfactions = " + satisfactions);
        logger.info(logTxt + "workId = " + workId);
        logger.info(logTxt + "ruleId = " + ruleId);
        logger.info(logTxt + "isHit = " + isHit);
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        logger.info(logTxt + "sortName = " + sortName);
        logger.info(logTxt + "sortOrder = " + sortOrder);
        try {
            return busWorkTestService.getPhoneDetailData(request);
        } catch (Exception e) {
            e.printStackTrace();
            return HtmlResult.failure("命中规则获取失败");
        }
    }

    @RequestMapping(value = "/export-phone-detail-data", method = RequestMethod.POST)
    public void exportPhoneDetailData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【模型测检明细-音频列表导出】";
        logger.info(logTxt);
        try {
            Map<String, Object> resMap = busWorkTestService.getPhoneDetailDataQuery(request);
            List<VoiceInfoDto> voiceInfos = (List<VoiceInfoDto>) resMap.get("data");
            logger.info(logTxt + "获取数据条数为：" + voiceInfos.size());
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "呼叫类型", "录音流水", "坐席班组", "坐席工号",  "坐席姓名", "客户号码",
                    "通话时长(秒)", "通话开始时间", "通话结束时间", "挂机方", "是否重复来电", "满意度"};
            String title = "模型测检明细_音频列表";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < voiceInfos.size(); j++) {
                VoiceInfoDto dto = voiceInfos.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString((j + 1) + ""));
                            break;
                        case 1:
                            cell2.setCellValue(new HSSFRichTextString(dto.getCallDirection() == 0 ? "呼入" : "呼出"));
                            break;
                        case 2:
                            cell2.setCellValue(new HSSFRichTextString(dto.getSerialNumber()));
                            break;
                        case 3:
                            cell2.setCellValue(new HSSFRichTextString(dto.getSeatGroup()));
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(dto.getSeatId()));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(dto.getSeatName()));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(dto.getCustomerNum()));
                            break;
                        case 7:
                            cell2.setCellValue(new HSSFRichTextString(dto.getHoldDuration() + ""));
                            break;
                        case 8:
                            cell2.setCellValue(new HSSFRichTextString(dto.getCallStartTime()));
                            break;
                        case 9:
                            cell2.setCellValue(new HSSFRichTextString(dto.getCallEndTime()));
                            break;
                        case 10:
                            cell2.setCellValue(new HSSFRichTextString(esUtil.changeOnHook(dto.getOnHook())));
                            break;
                        case 11:
                            cell2.setCellValue(new HSSFRichTextString(dto.getReCallFlag() == 0 ? "否" : "是"));
                            break;
                        case 12:
                            cell2.setCellValue(new HSSFRichTextString(esUtil.changeSatisfaction(dto.getSatisfaction())));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
